public class linkList<T> {
    /*头指针*/
    private Node<T> head;
    /*单链表的长度*/
    private int length;
    public linkList(){
        length = 0;
        head = new Node<T>(null);
    }
    public Node<T> getHead(){
        return head;
    }

    public boolean add(T obj,int pos){ //在链表中插入一个新的节点
        if (pos<1 || pos>length+1){
            System.out.println("pos值不合法");
            return false;
        }
        int num=1;
        Node<T> p = head,q=head.next;
        while (num<pos){
            p=q;
            q=q.next;
            num++;
        }
        p.next = new Node<T>(obj,q);
        length++;
        return true;
    }
    public T remove(int pos){   //删除一个节点
        if (isEmpty()){
            System.out.println("链表为空表");
            return null;
        }else {
            if (pos<1||pos>length+1){
                System.out.println("pos值不合法");
                return null;
            }
            int num=1;
            Node<T> p = head,q=head.next;
            while (num<pos){
                p=q;
                q=q.next;
                num++;
            }
            p.next=q.next;
            length--;
            return q.data;
        }

    }

    public int find(T obj){     //在链表中查找一个元素
        if (isEmpty()){
            System.out.println("链表为空");
            return -1;
        }
        int num=1;
        Node<T> p = head.next;
        while (p!=null){
            if (p.data.equals(obj)==false){
                p=p.next;
                num++;
            }
            else break;
        }
        if (p==null){
            return -1;
        }
        return num;
    }
    public T value(int pos){           //获取一个节点
        if (isEmpty()){
            System.out.println("链表为空");
            return null;
        }else {
            if (pos<1||pos>length+1){
                System.out.println("pos值不合法");
                return null;
            }
            int num=1;
            Node<T> q = head.next;
            while (num<pos){
                q=q.next;
                num++;
            }
            return q.data;
        }
    }
    public boolean isEmpty(){   //判空
        return length == 0;
    }
    public int size(){          //链表汇总数据元素的个数
        return length;
    }
    public void nextOrder()     //依次访问链表每个元素并输出
    {
        Node<T> p = head.next;
        while (p!=null){
            System.out.println(p.data);
            p=p.next;
        }
    }
    public void clear()         //销毁一个已经存在的链表
    {
        length=0;
        head.next=null;
    }

}
